/*
 * ESP8266智能小车控制代码
 * 功能：通过Wi-Fi网页控制小车前进/后退
 * 作者：Inscode AI助手
 * 日期：2025-10-16
 */

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

// WiFi配置
const char* ssid = "ESP8266_Car";     // WiFi热点名称
const char* password = "12345678";    // WiFi密码

// 电机控制引脚定义
const int motorA1 = D5;  // 左电机正转
const int motorA2 = D6;  // 左电机反转
const int motorB1 = D7;  // 右电机正转
const int motorB2 = D8;  // 右电机反转

// 创建Web服务器对象，监听80端口
ESP8266WebServer server(80);

// 电机控制函数
void stopCar() {
  digitalWrite(motorA1, LOW);
  digitalWrite(motorA2, LOW);
  digitalWrite(motorB1, LOW);
  digitalWrite(motorB2, LOW);
}

void moveForward() {
  digitalWrite(motorA1, HIGH);
  digitalWrite(motorA2, LOW);
  digitalWrite(motorB1, HIGH);
  digitalWrite(motorB2, LOW);
}

void moveBackward() {
  digitalWrite(motorA1, LOW);
  digitalWrite(motorA2, HIGH);
  digitalWrite(motorB1, LOW);
  digitalWrite(motorB2, HIGH);
}

// 处理HTTP请求的函数
void handleRoot() {
  String html = "<!DOCTYPE html>"
                "<html>"
                "<head>"
                "<meta name='viewport' content='width=device-width, initial-scale=1'>"
                "<title>ESP8266智能小车控制</title>"
                "<style>"
                "body { font-family: Arial; text-align: center; margin-top: 50px; background-color: #f0f0f0; }"
                ".container { max-width: 400px; margin: 0 auto; padding: 20px; background: white; border-radius: 10px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }"
                "h1 { color: #333; }"
                ".button { width: 120px; height: 60px; font-size: 18px; margin: 10px; border: none; border-radius: 5px; cursor: pointer; }"
                ".forward { background-color: #4CAF50; color: white; }"
                ".backward { background-color: #f44336; color: white; }"
                ".stop { background-color: #ff9800; color: white; }"
                ".status { margin-top: 20px; font-size: 20px; color: #666; }"
                "</style>"
                "</head>"
                "<body>"
                "<div class='container'>"
                "<h1>ESP8266智能小车</h1>"
                "<p>通过Wi-Fi控制小车移动</p>"
                "<button class='button forward' onclick=\"sendCommand('forward')\">前进</button><br>"
                "<button class='button backward' onclick=\"sendCommand('backward')\">后退</button><br>"
                "<button class='button stop' onclick=\"sendCommand('stop')\">停止</button>"
                "<div class='status' id='status'>准备就绪</div>"
                "</div>"
                "<script>"
                "function sendCommand(cmd) {"
                "  fetch('/' + cmd)"
                "    .then(response => response.text())"
                "    .then(data => {"
                "      document.getElementById('status').innerHTML = '状态: ' + (cmd == 'forward' ? '前进' : cmd == 'backward' ? '后退' : '停止');"
                "    });"
                "}"
                "</script>"
                "</body>"
                "</html>";
  server.send(200, "text/html", html);
}

void handleForward() {
  moveForward();
  server.send(200, "text/plain", "前进");
  delay(500);  // 前进0.5秒后停止
  stopCar();
}

void handleBackward() {
  moveBackward();
  server.send(200, "text/plain", "后退");
  delay(500);  // 后退0.5秒后停止
  stopCar();
}

void handleStop() {
  stopCar();
  server.send(200, "text/plain", "停止");
}

void setup() {
  Serial.begin(115200);
  
  // 设置电机引脚为输出模式
  pinMode(motorA1, OUTPUT);
  pinMode(motorA2, OUTPUT);
  pinMode(motorB1, OUTPUT);
  pinMode(motorB2, OUTPUT);
  
  // 初始化电机状态为停止
  stopCar();
  
  // 启动WiFi热点模式
  WiFi.softAP(ssid, password);
  Serial.println("WiFi热点已启动");
  Serial.print("IP地址: ");
  Serial.println(WiFi.softAPIP());
  
  // 设置HTTP请求处理函数
  server.on("/", handleRoot);
  server.on("/forward", handleForward);
  server.on("/backward", handleBackward);
  server.on("/stop", handleStop);
  
  // 启动Web服务器
  server.begin();
  Serial.println("HTTP服务器已启动");
}

void loop() {
  server.handleClient();
}
